

import util from "../js/util.js";
var app=getApp();

try {
    //process.npm
	if (dd.isIDE) {
    console.log('开发环境');
	} else {
    console.log('生产环境');
	};
}catch (e) {
	console.log(e);
};



export function post (url, params) {
  this.$Token = dd.getStorageSync("userMessage").Token;
  app.prototype.$Token = dd.getStorageSync("userMessage").Token;
  app.prototype.$SysPhone = dd.getStorageSync("userMessage").SysPhone;
  app.prototype.$SysUserName = dd.getStorageSync("userMessage").SysUserName;
  app.prototype.$SysUserCnName = dd.getStorageSync("userMessage").SysUserCnName;


	util.isNull(params) ? params.Token = this.$Token : params = {Token: this.$Token};
	return new Promise((reslove,reject) => {

		dd.httpRequest({
			url: url,
			header: {"Content-type": "application/x-www-form-urlencoded"},
			data: JSON.stringify(params),
			method: "POST",
			success: (res) => {
				dd.hideLoading();
				if (res.data.is == 1) {
					reslove(res);
				} else {
					reject(res);
				};
			},
			fail: err => {
				console.log(err);
			}
		});
	}).then(res => {
		return res.data;
	}).catch(err => {
    if (err.data.msg == "列表为空！" || err.data.msg == "填写格式错误，提交失败！") return false;
		let text = util.isNull(this.$Token) ? "请联系管理员" : "登录已过期";
		dd.showToast({
			title: err.data.msg || text,
			icon: "none",
			image: "../../static/img/fail.png",
			duration: 3000
		});
		// if (util.isNull(this.$Token)) uni.reLaunch({url: "/pages/Home/Home.vue"});
		return err;
	})
}

export function postFile (url, params) {
	return new Promise((reslove, reject) => {
    
		dd.uploadFile({
			url: url,
			name: "file",
      formData: params,
      filePath: params.file.path,
			success: res => {
        reslove(res)
      }
    });
	})
}

Object.fromEntries = function fromEntries (iterable) {
  return [...iterable].reduce((obj, [key, val]) => {
      obj[key] = val;
      return obj;
  }, {});
};